package zxing.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.crud.DataSupport;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.virtualapp.R;
import io.virtualapp.abs.ui.VActivity;
import io.virtualapp.bean.MyCardBean;

public class MyCardActivity extends VActivity {
    private EditText et_only_company;
    private EditText et_only_position;
    private EditText et_only_phone;
    private EditText et_only_email;
    private EditText et_only_web1;
    private EditText et_only_add;
    private EditText et_only_note;
    private EditText et_only_name;
    @BindView(R.id.head_back)
    ImageView head_back;
    @BindView(R.id.head_center_title)
    TextView head_center_title;
    private List<MyCardBean> datas;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_card);
        ButterKnife.bind(this);
        et_only_company = findViewById(R.id.et_only_company);
        et_only_position = findViewById(R.id.et_only_position);
        et_only_phone = findViewById(R.id.et_only_phone);
        et_only_email = findViewById(R.id.et_only_email);
        et_only_web1 = findViewById(R.id.et_only_web1);
        et_only_add = findViewById(R.id.et_only_add);
        et_only_note = findViewById(R.id.et_only_note);
        et_only_name = findViewById(R.id.et_only_name);
        initView();
    }

    private void initView() {
        head_center_title.setText("我的名片");
        datas = new ArrayList<>();
        datas = DataSupport.findAll(MyCardBean.class);
        if (datas != null && !datas.isEmpty()) {
            et_only_name.setText(datas.get(0).getName());
            et_only_company.setText(datas.get(0).getCompany());
            et_only_position.setText(datas.get(0).getPosition());
            et_only_phone.setText(datas.get(0).getPhone());
            et_only_email.setText(datas.get(0).getEmail());
            et_only_add.setText(datas.get(0).getAdd());
            et_only_note.setText(datas.get(0).getNote());
        }
        head_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.but).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (TextUtils.isEmpty(et_only_name.getText().toString())) {
                    Toast.makeText(MyCardActivity.this, "姓名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                String name = et_only_name.getText().toString().trim();
                String company = et_only_company.getText().toString().trim();
                String position = et_only_position.getText().toString().trim();
                String phone = et_only_phone.getText().toString().trim();
                String email = et_only_email.getText().toString().trim();
                String web1 = et_only_web1.getText().toString().trim();
                String add = et_only_add.getText().toString().trim();
                String note = et_only_note.getText().toString().trim();
                String contents = "BEGIN:VCARD\nVERSION:3.0\n" + "FN:" + name
                        + "\nORG:" + company + "\nTITLE:" + position
                        + "\nNOTE:" + note + "\nTEL:" + phone + "\nADR:" + add
                        + "\nEMAIL:" + email + "\nEND:VCARD";
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
                Date date = new Date(System.currentTimeMillis());
                String n = simpleDateFormat.format(date);
                MyCardBean user = new MyCardBean();
                if (DataSupport.findFirst(MyCardBean.class) != null) {
                    DataSupport.deleteAll(MyCardBean.class);
                }
                user.setName(name);
                user.setCompany(company);
                user.setPosition(position);
                user.setPhone(phone);
                user.setEmail(email);
                user.setAdd(add);
                user.setNote(note);
                user.setContents(contents);
                user.save();
                finish();
//                Intent intent = new Intent(getActivity(), CodeDisplayActivity.class);
//                intent.putExtra("data", contents);
//                intent.putExtra("title", "我的名片");
//                startActivity(intent);

            }
        });
    }
}
